package org.domain.sigepac.session;

import java.util.List;
import java.util.Set;

import org.domain.sigepac.Utils;
import org.domain.sigepac.Entity.CadastroReserva;
import org.domain.sigepac.Entity.Curso;
import org.domain.sigepac.Entity.Turma;
import org.domain.sigepac.Entity.Voluntario;
import org.domain.sigepac.home.EntityHomeSupport;
import org.domain.sigepac.repository.RepositorioAluno;
import org.domain.sigepac.repository.RepositorioCurso;
import org.domain.sigepac.repository.RepositorioTurma;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.FlushModeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.international.StatusMessages;

@Name("turmaHome")
@Scope(ScopeType.CONVERSATION)
public class TurmaHome extends EntityHomeSupport<Turma> {

	private static final long serialVersionUID = 1624266645825051326L;

	@In
	private StatusMessages statusMessages;

	@In
	private RepositorioTurma repositorioTurma;

	@In
	private RepositorioCurso repositorioCurso;

	@In
	private RepositorioAluno repositorioAluno;

	private Voluntario voluntario;

	@DataModel
	private Set<Voluntario> listarVoluntarios;

	public TurmaHome() {
		// TODO Auto-generated constructor stub
	}


	@Override
	protected Turma createInstance() {
		Turma turma=new Turma();
		return turma;
	}

	@Factory("listarVoluntarios")
	public void listarVoluntarios(){
		listarVoluntarios=getInstance().getVoluntarios();
	}

	public void trocaCidade(){
		if(getInstance().getCampus()!=null)
			getInstance().setCidade(getInstance().getCampus().getCidade());
		else
			getInstance().setCidade(null);
	}


	@End
	public void salvar(){
		boolean check=false;
		if(!isManaged()){
			check=true;
		}
		repositorioTurma.inserirOuAtualizar(getInstance());
		if(check){
			Curso curso=getInstance().getCurso();
			if(curso.isInclusaoDigital()){
				if(curso.isTemReserva()){

					List<CadastroReserva> cadastrosReserva=curso.getCadastrosReserva();
					for(CadastroReserva cadastroReserva:cadastrosReserva){
						if(getInstance().temVagas()){
							cadastroReserva.getAluno().getTurmas().add(getInstance());
							repositorioAluno.atualizar(cadastroReserva.getAluno());
							curso.getCadastrosReserva().remove(cadastroReserva);
						}else
							break;
					}
					repositorioCurso.atualizar(curso);
				}
			}
		}
		Utils.entityManagerFlush();
		createdMessage();

	}

	public void adicionarVoluntario(){
		getInstance().getVoluntarios().add(getVoluntario());
		listarVoluntarios();
	}

	public void removerVoluntario(Voluntario voluntario){
		getInstance().getVoluntarios().remove(voluntario);
		listarVoluntarios();
	}

	@Begin(flushMode=FlushModeType.MANUAL,join=true)
	public void setIdTurma(Integer id) {
	    setId(id);
	 }

	public Integer getIdTurma() {
	    return (Integer) getId();
	  }

	public boolean isWired(){
		return getInstance().getId()!=null;
	}

	public Voluntario getVoluntario() {
		if(voluntario==null)
			voluntario=new Voluntario();
		return voluntario;
	}

	public void setVoluntario(Voluntario voluntario) {
		this.voluntario = voluntario;
	}
}
